﻿@{
    ViewData["Title"] = "SM2";
}
<h1>@ViewData["Title"]</h1>

<div>
    <div>  公钥：<span id="publicKey"></span></div>
    <div>  私钥：<span id="privateKey"></span></div>
    <div>  内容：<textarea id="content" style="width:600px;">www.ruovea.com</textarea> </div>
    <div>  加密后：<textarea id="outtxt" style="width:600px;"></textarea> </div>

    <div>
        <input type="button" value="加密" onclick="doEncrypt();" />
        <input type="button" value="解密" onclick="doDecrypt();" />
    </div>
    <div>
        <input type="button" value="验签1" onclick="doSignature1();" />
        <input type="button" value="验签2" onclick="doSignature2();" />
        <input type="button" value="验签3" onclick="doSignature3();" />
        <input type="button" value="验签4" onclick="doSignature4();" />
        <input type="button" value="验签5" onclick="doSignature5();" />
        <span id="verifyresult" style="color:green"></span>
    </div>
    <hr />
    <div>
        <br /><span>pubKey:</span>@ViewBag.pubKey
        <br /><span>priKey:</span>@ViewBag.priKey
        <br /><span>结果:</span>@ViewBag.rus
        <input type="button" value="加密" onclick="Encrypt();" />
        <textarea id="outtxt2" style="width:600px;"></textarea>
    </div>
</div>


@section Scripts {
@{
    await Html.RenderPartialAsync("_Sm234ScriptsPartial");
 }

<script>
    const cipherMode = 0; // 1 - C1C3C2，0 - C1C2C3

    let publicKey;
    let privateKey;
    beforeAll();
    function beforeAll() {
        // 生成密钥对
        let keypair = sm2.generateKeyPairHex();
        publicKey = keypair.publicKey;//130
        $("#publicKey").html(publicKey);
        privateKey = keypair.privateKey;//64
        $("#privateKey").html(privateKey);
    }

     function Encrypt(){
        var msgString=$("#content").val();
        let encryptData = sm2.doEncrypt(msgString,"@ViewBag.pubKey", cipherMode);
        $("#outtxt2").val(encryptData);
    }

    function doEncrypt(){
        var msgString=$("#content").val();
        let encryptData = sm2.doEncrypt(msgString, publicKey, cipherMode);
        $("#outtxt").val(encryptData);
    }
    function doDecrypt(){
        var msgString=$("#content").val();
        let decryptData = sm2.doDecrypt(msgString, privateKey, cipherMode);
        $("#outtxt").val(decryptData);
    }
    function doSignature1(){
        // 纯签名 + 生成椭圆曲线点
        var msgString=$("#content").val();
        let sigValueHex = sm2.doSignature(msgString, privateKey);
        let verifyResult = sm2.doVerifySignature(msgString, sigValueHex, publicKey);
        console.log(verifyResult)
         $("#verifyresult").html(verifyResult);
    }
    function doSignature2(){ // 纯签名
        var msgString=$("#content").val();
        let sigValueHex2 = sm2.doSignature(msgString, privateKey, {
            pointPool: [sm2.getPoint(), sm2.getPoint(), sm2.getPoint(), sm2.getPoint()],
        });
        let verifyResult2 = sm2.doVerifySignature(msgString, sigValueHex2, publicKey);

        $("#verifyresult").html(verifyResult2);
    }
    function doSignature3(){
     // 纯签名 + 生成椭圆曲线点 + der编解码
        var msgString=$("#content").val();
        let sigValueHex3 = sm2.doSignature(msgString, privateKey, {
            der: true,
        });
        let verifyResult3 = sm2.doVerifySignature(msgString, sigValueHex3, publicKey, {
            der: true,
        });

         $("#verifyresult").html(verifyResult3);
    }
    function doSignature4(){
      // 纯签名 + 生成椭圆曲线点 + sm3杂凑
        var msgString=$("#content").val();
        let sigValueHex4 = sm2.doSignature(msgString, privateKey, {
            hash: true,
        });
        let verifyResult4 = sm2.doVerifySignature(msgString, sigValueHex4, publicKey, {
            hash: true,
        });

         $("#verifyresult").html(verifyResult4);
    }
    function doSignature5(){
        // 纯签名 + 生成椭圆曲线点 + sm3杂凑（不做公钥推导）
        var msgString=$("#content").val();
        let sigValueHex5 = sm2.doSignature(msgString, privateKey, {
            hash: true,
            publicKey,
        });
        let verifyResult5 = sm2.doVerifySignature(msgString, sigValueHex5, publicKey, {
            hash: true,
            publicKey,
        });

         $("#verifyresult").html(verifyResult5);
    }
</script>
}